<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Expansion\Calculation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CalculationService
{

    use Service;

    /**
     * 计算器首页
     *
     * @return mixed
     */
    public function CalculationIndexService()
    {
        $select      = [
            'id', 'buy', 'sale', 'number', 'results', 'fee', 'auction', 'count', 'category', 'updated_at',
        ];
        $calculation = Calculation::PaginateModel(1, $select);
        $pt          = Calculation::PaginateModel(2, $select);
        return view('expansion.calculation.index', compact('calculation', 'pt'));
    }

    /**
     * DNF计算器
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CalculationMaxService( $request )
    {
        $validator = $this->getAdminValidator()->CalculationValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $sale   = $request->input('sale');
        $buy    = $request->input('buy');
        $number = $request->input('number');
        $max    = $request->input('max');
        $num    = calculationNum($sale, $buy, '-');
        $num    = calculationNum($num, $number, '×');
        switch ( $max ) {
            case 'yes':
                $res[ 'auction' ]  = 1;
                $data[ 'fee' ]     = calculationNum(calculationNum($sale, $number, '×'), 0.03, '×');
                $data[ 'results' ] = calculationNum($num, $data[ 'fee' ], '-');
                break;
            default:
                $res[ 'auction' ]  = 2;
                $data[ 'fee' ]     = calculationNum(calculationNum($sale, $number, '×'), 0.05, '×');
                $data[ 'results' ] = calculationNum($num, $data[ 'fee' ], '-');
                break;
        }
        $where = function ( $query ) use ( $number, $buy, $sale, $max ) {
            $query->where('buy', $buy);
            $query->where('sale', $sale);
            $query->where('number', $number);
            $name = $max == 'yes' ? (int) 1 : (int) 2;
            $query->where('auction', $name);
        };
        $id    = Calculation::ValueModel($where, 'id');
        if ( ! $id ) {
            $res[ 'buy' ]     = $buy;
            $res[ 'sale' ]    = $sale;
            $res[ 'number' ]  = $number;
            $res[ 'fee' ]     = $data[ 'fee' ];
            $res[ 'results' ] = $data[ 'results' ];
            $res[ 'count' ]   = 1;
            $res[ 'uid' ]     = Auth::id();
            Calculation::CreateModel($res);
        }
        else Calculation::IncrementModel($id, 'count');
        return data($data);
    }

    /**
     * 普通计算器
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CalculationStoreService( $request )
    {
        $status  = $request->input('status');
        $results = $request->input('results');
        $name    = explode($status, $results);
        if ( ! $name[ 0 ] || ! $name[ 1 ] ) return message($this->getErrorCode()::message(1001));
        $name = calculationNum($name[ 0 ], $name[ 1 ], $status);
        if ( ! $name ) return message('error');
        $where = function ( $query ) use ( $results ) {
            $query->where('sale', $results);
        };
        $id    = Calculation::ValueModel($where, 'id');
        if ( ! $id ) {
            $res[ 'sale' ]     = $results;
            $res[ 'results' ]  = $name;
            $res[ 'count' ]    = 1;
            $res[ 'category' ] = 2;
            Calculation::CreateModel($res);
        }
        else Calculation::IncrementModel($id, 'count');
        return data($name);
    }

    /**
     * 删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CalculationDeleteService( $id )
    {
        Calculation::DestroyModel($id);
        return data();
    }

}
